<template>
	<view class="page">
		<view class="tab1" v-show="tabValue == 0">
			<view class="page__head">
				<view class="page__head--right">
					<view class="page__head--right-box" @click="goDraft">
						<view class="">草稿箱</view>
						<view class="">({{draftNum}})</view>
						<view class="" style="margin-top: 3rpx;">
							<u-icon name="arrow-right" size="15" color="#1677ff"></u-icon>
						</view>
					</view>
				</view>
				<view class="page__head--left">
					<view class="page__head--left__icon center">
						<u-icon name="search" size="25" @click="goSeach"></u-icon>
					</view>
					<view class="page__head--left__icon center">
						<c-sort @change="changeMode"></c-sort>
					</view>
				</view>
			</view>
			<view class="page__category">
				<view class="page__category--list">
					<view class="page__category--list-item" :class="category_index == index ? 'category-active' : ''" v-for="(item, index) in categories" :key="index" @click="changeCategory(index)">{{item.TypeName}}</view>
				</view>
				<u-popup mode="top" :overlay="false" duration="0" :customStyle="{marginTop: '200rpx'}" :show="categoryVisible" @close="categoryVisible = false" @open="categoryVisible = true">
					<view class="category-pop">
						<view class="category-pop__head">
							<view class="category-pop__head--title">全部分类</view>
							<view class="category-pop__head--icon">
								<u-icon name="close" size="20" @click="categoryVisible = false"></u-icon>
							</view>
						</view>
						<view class="category-pop__content">
							<view class="category-pop__content--list" :class="category_index == index ? 'category-active' : ''" v-for="(item, index) in categories" :key="index" @click="changeCategory(index)">{{item.TypeName}}</view>
							<view class="category-pop__content--list" style="background-color: #fff;" v-for="item in (3 - categories.length % 3)" v-show="categories.length % 3 != 0"></view>
						</view>
					</view>
				</u-popup>
				<view class="page__category--icon" @click="openAllCategory">
					<u-icon name="grid" size="25"></u-icon>
				</view>
			</view>
			<view class="content">
				<view class="list" v-for="(item,index) in list" :key="item.Id" @click="openForm(item)">
					<view class="list__cover">
						<image :src="item.cover" mode="aspectFill"></image>
					</view>
					<view class="list__title ellipsis">{{item.FormTitle}}</view>
				</view>
				<view class="list" v-if="list.length % 2 == 1"></view>
			</view>
			<view class="no-more" v-if="noMore">没有更多了</view>
			<no-data v-if="noData"></no-data>
		</view>
		<view class="tab2" v-show="tabValue == 1">
			<view class="form" v-for="item in myForms" :key="item.Id" @click="preview(item)">
				<view class="form__body">
					<view class="form__body--cover">
						<image :src="item.cover" mode="aspectFill"></image>
					</view>
					<view class="form__body--content">
						<view class="form__body--content-title ellipsis">{{item.FormTitle}}</view>
						<view class="form__body--content-des ellipsis">{{item.FormDesc}}</view>
						<view class="form__body--content-des">{{item.CreateTime}}</view>
					</view>
					<view class="form__body--right">
						<view class="form__body--right-btn" @click.stop="goMyForm(item)">修改</view>
					</view>
				</view>
			</view>
		</view>
		
		<u-tabbar
			:value="tabValue"
			@change="tabChange"
		>
			<u-tabbar-item text="模版" icon="file-text-fill"></u-tabbar-item>
			<u-tabbar-item text="已填写" icon="edit-pen-fill"></u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js';
	import _data from '@/common/_data.js';
	import cSort from '@/components/custom/c-sort/c-sort.vue'
	import NoData from '@/components/custom/no-data/no-data.nvue'
	
	export default {
		components: {
			cSort,
			NoData
		},
		data() {
			return {
				static_img: _data.data('static_img'),
				obsusername: _data.localData('name'),
				pxid: _data.localData('userid'),
				list: [],
				categories: [],
				category: '',
				category_index: 0,
				categoryVisible: false,
				draftNum: "",
				mode: 'asc',
				currentpage: 1,
				pagesize: 10,
				noMore: false,
				noData: false,
				tabValue: 0,
				myForms: []
			}
		},
		onLoad() {
			let that = this;
			this.totalInit();
			this.getSmartFormType();
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: '/pages/home/child/smart-form-add/index'
			})
		},
		onReachBottom() {
			if(!this.noMore) {
				this.currentpage += 1;
				this.getSmartFormList();
			}
		},
		methods: {
			init() {
				this.list = [];
				this.noMore = false;
				this.noData = false;
				this.currentpage = 1;
			},
			leftMenuInit() {
				this.init();
				this.getSmartFormList();
				this.getSmartFormDraftCount();
			},
			rightMenuInit() {
				this.getSmartFormMyDataList();
			},
			totalInit() {
				this.leftMenuInit();
				this.rightMenuInit();
			},
			/**
			 * 获取表单模板类型
			 * @param {Object} v
			 */
			getSmartFormType(v) {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormType',
					success(res) {
						res.unshift({
							TypeName: '全部'
						})
						that.categories = res;
					},
				})
			},
			/**
			 * 获取动态表单草稿箱数量
			 * @param {Object} v
			 */
			getSmartFormDraftCount(v) {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormDraftCount',
					apiparam: {
						createpxid: this.pxid
					},
					success(res) {
						if(!res.length) return
						that.draftNum = res[0].DraftCount;
					},
				})
			},
			/**
			 * 获取表单模板列表
			 * @param {Object} v
			 */
			getSmartFormList() {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormList',
					pagesize: this.pagesize,
					currentpage: this.currentpage,
					apiparam: {
						keyword: "", //关键词模糊查询
						typename: this.category,
						sorting: this.mode, //asc:最早，desc:最新
					},
					success(res) {
						if(res.length < that.pagesize) that.noMore = true;
						if(that.currentpage == 1 && !res.length) {
							that.noMore = false;
							that.noData = true;
							that.list = [];
							return
						}
						
						that.noData = false;
						res.forEach(item => {
							item.cover = that.static_img + '/' + item.CoverImage;
						})
						that.list.push(...res);
					},
				})
			},
			/**
			 * 获取我填写过的业务表单列表
			 */
			getSmartFormMyDataList() {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormMyDataList',
					apiparam: {
						obsusername: this.obsusername,
						obspxid: this.pxid,
					},
					success(res) {
						if(!res.length) return
						res.forEach(item => {
							item.cover = that.static_img + '/' + item.CoverImage;
						})
						that.myForms= res;
					},
				})
			},
			openAllCategory() {
				this.categoryVisible = true;
			},
			changeCategory(i) {
				this.category_index = i;
				let value = this.categories[i].TypeName;
				value = value == '全部' ? '' : value;
				this.category = value;
				this.leftMenuInit();
				this.categoryVisible = false;
			},
			goDraft() {
				uni.navigateTo({
					url: './draft'
				})
			},
			openForm(v) {
				let obj = JSON.parse(JSON.stringify(v));
				let json = v.FormJson.replace(/%22/g,'"');
				try {
					obj.FormJson = JSON.parse(json);
				} catch(e) {
					console.log(e)
				}
				uni.navigateTo({
					url: '../smart-form-add/view?type=2&data=' + JSON.stringify(obj)
				})
			},
			goMyForm(v) {
				let obj = JSON.parse(JSON.stringify(v));
				let json = v.DataJson.replace(/%22/g,'"');
				try {
					obj.DataJson = JSON.parse(json);
				} catch(e) {
					console.log(e)
				}
				uni.navigateTo({
					url: '/pages/home/child/smart-form-add/view-self?data=' + JSON.stringify(obj)
				})
			},
			preview(v) {
				let obj = JSON.parse(JSON.stringify(v));
				let json = v.DataJson.replace(/%22/g,'"');
				try {
					obj.FormJson = JSON.parse(json);
					obj.DataJson = {};
				} catch(e) {
					console.log(e)
				}
				console.log(obj)
				uni.navigateTo({
					url: '/pages/home/child/smart-form-add/view?type=1&data=' + JSON.stringify(obj)
				})
			},
			goSeach() {
				uni.navigateTo({
					url: './search',
					animationType: 'none'
				})
			},
			changeMode(e) {
				this.mode = e;
				this.init();
				this.getSmartFormList();
			},
			tabChange(e) {
				if(this.tabValue == e) return
				this.tabValue = e;
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		
		&__head {
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			/* #ifdef H5 */
			top: 100rpx;
			/* #endif */
			z-index: 9;
			height: 100rpx;
			padding: 0 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			&--left {
				display: flex;
				
				&__icon {
					
				}
			}
			
			&--right {
				
				&-box {
					padding: 10rpx;
					border-radius: 10rpx;
					background-color: #bae0ff;
					color: #1677ff;
					display: flex;
					align-items: center;
				}
			}
		}
		
		&__category {
			position: fixed;
			left: 0;
			right: 0;
			top: 100rpx;
			/* #ifdef H5 */
			top: 200rpx;
			/* #endif */
			z-index: 9;
			height: 100rpx;
			padding-left: 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			&--list {
				width: 650rpx;
				display: flex;
				overflow-x: scroll;
				flex-wrap: nowrap;
				align-items: center;
				
				&-item {
					padding: 0 20rpx;
					font-size: 33rpx;
					flex-shrink: 0;
				}
			}
			
			&--icon {
				width: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
	
	.content {
		margin-top: 200rpx;
		padding: 0 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	
	.list {
		width: 340rpx;
		border-radius: 20rpx;
		flex-shrink: 0;
		margin-top: 40rpx;
		
		&__cover {
			height: 180rpx;
			
			image {
				width: 100%;
				height: 100%;
				border-radius: 20rpx 20rpx 0 0;
			}
		}
		
		&__title {
			height: 80rpx;
			font-size: 35rpx;
			padding: 0 20rpx;
			line-height: 80rpx;
			background-color: #fff;
			border-radius: 0 0 20rpx 20rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}
	
	.category-pop {
		background-color: #fff;
		
		&__head {
			height: 100rpx;
			margin: 0 20rpx;
			box-sizing: border-box;
			position: relative;
			border-bottom: 1rpx solid #eee;
			
			&--title {
				text-align: center;
				line-height: 100rpx;
			}
			
			&--icon {
				position: absolute;
				right: 20rpx;
				top: 30rpx;
			}
		}
		
		&__content {
			display: flex;
			justify-content: space-around;
			flex-wrap: wrap;
			padding: 20rpx;
			
			&--list {
				width: 200rpx;
				height: 60rpx;
				font-size: 33rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 20rpx;
				background-color: #f0f0f0;
				margin-top: 20rpx;
			}
		}
	}
	
	.form {
		background-color: #f0f0f0;
		padding: 20rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		margin: 20rpx 0;
		
		&__body {
			display: flex;
			height: 100%;
			
			&--cover {
				width: 150rpx;
				height: 150rpx;
				
				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
			
			&--content {
				margin-left: 20rpx;
				width: calc(100% - 150rpx - 120rpx);
				display: flex;
				flex-direction: column;
				font-size: 25rpx;
				
				&-title {
					font-size: 32rpx;
					width: 100%;
				}
				
				&-des {
					font-size: 27rpx;
					color: #aaa;
					height: 50rpx;
				}
				
				text {
					font-size: 25rpx;
				}
			}
			
			&--tag {
				display: flex;
			}
			
			&--right {
				width: 120rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				
				&-btn {
					color: #fff;
					width: 100%;
					height: 70rpx;
					border-radius: 35rpx;
					background-color: #1677ff;
					text-align: center;
					line-height: 70rpx;
				}
			}
		}
		
		
	}
	
	.category-active {
		color: #91caff;
	}
	
	.no-more {
		height: 100rpx;
		text-align: center;
		line-height: 100rpx;
	}
</style>
